#include "i18n.h"
#include "ASScrList.h"
#include "ASScreenMgr.h"
#include "..\app\AsAppImgInfo.h"
#include "ASScrMain.h"
#include "res_str.h"
#include "ASScrDetail.h"
#include "mrc_advbar.h"
#include "..\app\ASLogic.h"
#include "ASScreenShare.h"
#include "..\app\ASAppConst.h"
#include "ASScrMain.h"
#include "ASWaitingScreen.h"
#include "ASScrCategory.h"

static as_list_screen* s_scr_list_p;
static ASINT s_category_id;



static void as_scr_list_add_head(as_screen* screen_p, ASINT categoryid)
{
    as_category_title* title_p;
    ASINT strid;

    if (categoryid < ENUM_CATEGORY_COUNT)
    {
        title_p = as_logic_get_category_title(categoryid);
        strid = title_p->title_cn;
    }
    else if (categoryid == ENUM_RECORD_TODO)
        strid = STR_TODO;
    else if (categoryid == ENUM_RECORD_FAVORITE)
        strid = STR_FAVORITE;
    else if (categoryid == ENUM_RECORD_TRIED)
        strid = STR_TRIED;
    else if(categoryid == ENUM_RECORD_UNTRIED)
        strid = STR_UNTRIED;
    else if (categoryid == ENUM_RECORD_RECENT)
        strid = STR_RECENT;
    else if (categoryid == ENUM_RECORD_ALL)
        strid = STR_ALL;

    s_scr_list_p->head_height = as_screen_create_adv_titlebar(screen_p, strid);
}

static void as_scr_list_pen_up_cb(as_listitem* item_p)
{
    ASINT idx = s_scr_list_p->lt_content->get_idx_from_item((as_control*)s_scr_list_p->lt_content, item_p);
    as_screen_manager* scr_mgr = as_get_screen_stack_instance();
    idx--;

    if (idx < 0 || idx >= AS_ITEMS_COUNT)
        return;

    idx = as_logic_translate_idx(idx, s_category_id);
    scr_mgr->clear_all();
    as_scr_detail_show(idx, s_category_id);
}

static ASINT as_ctrl_list_item_if_need_hide(as_list* self, as_listitem* item_p)
{
    if(!self || !item_p)
        return TRUE;

    if(item_p->base.area.y + item_p->base.area.h <= self->base.area.y
       || item_p->base.area.y >= self->base.area.y + self->base.area.h)
    {
        return TRUE;
    }
    else
    {
        return FALSE;
    }
}

static void as_scr_list_add_category_items(as_list* list_p, ASINT ibegin, ASINT iend)
{
    ASUINT param[4];
    ASINT  i, j;
    as_listitem* item;
    as_label* lb_title;
    as_image* img_p;
    as_item_content* item_content;
    as_rect area;
    ASINT screen_w, item_y, font_h;
    screen_w = as_get_lcd_width();
    font_h = as_get_font_width();

    for (i = 0, j = ibegin; j < iend; i++, j++)
    {
        item_content = as_logic_get_item(j);
        item_y = s_scr_list_p->head_height + IMG_LIST_ITEM_T_H + i * IMG_LIST_ITEM_H;
        area.x = 0;
        area.y = item_y;
        area.w = screen_w;
        area.h = IMG_LIST_ITEM_H;
        item = s_scr_list_p->lt_content->add_item((as_control*)s_scr_list_p->lt_content, area);
        item->set_bg_image((as_control*)item, as_ucs2_from_ascii(IMG_LIST_ITEM), AS_IMAGE_FROM_RES);
        item->set_bg_selected_image((as_control*)item, as_ucs2_from_ascii(IMG_LIST_ITEM_D), AS_IMAGE_FROM_RES);
        item->set_pen_up_cb((as_control*)item, as_scr_list_pen_up_cb);
        area.x = SCRLIST_ITEM_SIDE_GAP;
        area.y = item_y + (IMG_LIST_ITEM_H - IMG_ITEM_PS1_H) / 2;
        area.w = IMG_ITEM_PS1_W;
        area.h = IMG_ITEM_PS1_H;
        param[0] = AS_IMAGE_FROM_RES;
        param[1] = (ASUINT)as_ucs2_from_ascii(item_content->img_small);
        param[2] = ASFALSE;
        img_p = (as_image*)item->add_ctrl((as_control*)item, AS_CTRL_IMAGE, area, 3, (void**)param);
        //img_p->set_if_trans((as_control*)img_p, ASTRUE);
        area.x = SCRLIST_ITEM_SIDE_GAP + IMG_ITEM_PS1_W + SCRLIST_ITEM_MID_GAP;
        area.y = item_y + (IMG_LIST_ITEM_H - font_h) / 2;
        area.w = as_get_wstr_width((ASWSTR)SGL_LoadString(item_content->title_cn));
        area.h = font_h;
        param[0] = (ASUINT)SGL_LoadString(item_content->title_cn);
        lb_title = (as_label*)item->add_ctrl((as_control*)item, AS_CTRL_LABEL, area, 1, (void**)param);
        lb_title->set_bg_hidden((as_control*)lb_title, ASTRUE);
        lb_title->set_text_color((as_control*)lb_title, 0x0);

        if (item_content->btried)
        {
            area.x = screen_w - SCRLIST_ITEM_SIDE_GAP - IMG_ITEM_INDICATOR_W;
            area.y = item_y + (IMG_LIST_ITEM_H - IMG_ITEM_INDICATOR_H) / 2;
            area.w = IMG_ITEM_INDICATOR_W;
            area.h = IMG_ITEM_INDICATOR_H;
            param[0] = AS_IMAGE_FROM_RES;
            param[1] = (ASUINT)as_ucs2_from_ascii(IMG_ITEM_INDICATOR);
            param[2] = ASFALSE;
            img_p = (as_image*)item->add_ctrl((as_control*)item, AS_CTRL_IMAGE, area, 3, (void**)param);
            //img_p->set_if_trans((as_control*)img_p, ASTRUE);
        }

        s_scr_list_p->lt_content->add_item_end((as_control*)s_scr_list_p->lt_content, item);
        s_scr_list_p->items_height += IMG_LIST_ITEM_H;
    }
}

static void as_scr_list_add_record_items(as_list* list_p, ASUINT8* records_p)
{
    ASUINT param[4], i, j;
    as_listitem* item;
    as_label* lb_title;
    as_image* img_p;
    as_item_content* item_content;
    as_rect area;
    ASINT screen_w, item_y, font_h;
    screen_w = as_get_lcd_width();
    font_h = as_get_font_width();

    for (i = 0, j = 0; i < AS_ITEMS_COUNT; i++)
    {
        if (records_p[i] == AS_UNVALID_ITEM_ID)
            continue;

        item_content = as_logic_get_item(records_p[i]);
        item_y = s_scr_list_p->head_height + IMG_LIST_ITEM_T_H + j * IMG_LIST_ITEM_H;
        area.x = 0;
        area.y = item_y;
        area.w = screen_w;
        area.h = IMG_LIST_ITEM_H;
        item = list_p->add_item((as_control*)list_p, area);
        item->set_bg_image((as_control*)item, as_ucs2_from_ascii(IMG_LIST_ITEM), AS_IMAGE_FROM_RES);
        item->set_bg_selected_image((as_control*)item, as_ucs2_from_ascii(IMG_LIST_ITEM_D), AS_IMAGE_FROM_RES);
        item->set_pen_up_cb((as_control*)item, as_scr_list_pen_up_cb);
        area.x = SCRLIST_ITEM_SIDE_GAP;
        area.y = item_y + (IMG_LIST_ITEM_H - IMG_ITEM_PS1_H) / 2;
        area.w = IMG_ITEM_PS1_W;
        area.h = IMG_ITEM_PS1_H;
        param[0] = AS_IMAGE_FROM_RES;
        param[1] = (ASUINT)as_ucs2_from_ascii(item_content->img_small);
        param[2] = ASFALSE;
        img_p = (as_image*)item->add_ctrl((as_control*)item, AS_CTRL_IMAGE, area, 3, (void**)param);
        //img_p->set_if_trans((as_control*)img_p, ASTRUE);
        area.x = SCRLIST_ITEM_SIDE_GAP + IMG_ITEM_PS1_W + SCRLIST_ITEM_MID_GAP;
        area.y = item_y + (IMG_LIST_ITEM_H - font_h) / 2;
        area.w = as_get_wstr_width((ASWSTR)SGL_LoadString(item_content->title_cn));
        area.h = font_h;
        param[0] = (ASUINT)SGL_LoadString(item_content->title_cn);
        lb_title = (as_label*)item->add_ctrl((as_control*)item, AS_CTRL_LABEL, area, 1, (void**)param);
        lb_title->set_bg_hidden((as_control*)lb_title, ASTRUE);
        lb_title->set_text_color((as_control*)lb_title, 0x0);

        if (item_content->btried)
        {
            area.x = screen_w - SCRLIST_ITEM_SIDE_GAP - IMG_ITEM_INDICATOR_W;
            area.y = item_y + (IMG_LIST_ITEM_H - IMG_ITEM_INDICATOR_H) / 2;
            area.w = IMG_ITEM_INDICATOR_W;
            area.h = IMG_ITEM_INDICATOR_H;
            param[0] = AS_IMAGE_FROM_RES;
            param[1] = (ASUINT)as_ucs2_from_ascii(IMG_ITEM_INDICATOR);
            param[2] = ASFALSE;
            img_p = (as_image*)item->add_ctrl((as_control*)item, AS_CTRL_IMAGE, area, 3, (void**)param);
            //img_p->set_if_trans((as_control*)img_p, ASTRUE);
        }

        list_p->add_item_end((as_control*)list_p, item);
        s_scr_list_p->items_height += IMG_LIST_ITEM_H;
        j++;
    }
}

static void as_scr_list_add_content_list_items(as_screen* screen_p, ASINT categoryid)
{
    as_rect area;
    ASUINT8* record_p = ASNULL;
    as_listitem* item;
    ASINT screen_w, font_h;
    ASINT idx_begin, idx_end, bRecord = ASFALSE;
    screen_w = as_get_lcd_width();
    font_h = as_get_font_height();

    if (categoryid > ENUM_CATEGORY_COUNT && categoryid < ENUM_RECORD_COUNT)
    {
        if(as_logic_get_record_count(categoryid) <= 0)
            return;
        else
            record_p = as_logic_get_record(categoryid);

        bRecord = ASTRUE;
    }
    else if (categoryid == ENUM_RECORD_ALL)
    {
        bRecord = ASFALSE;
        idx_begin = 0;
        idx_end = AS_ITEMS_COUNT - 1;
    }
    else
    {
        as_logic_get_category(categoryid, &idx_begin, &idx_end);
        bRecord = ASFALSE;
    }

    area.x = 0;
    area.y = s_scr_list_p->head_height;
    area.w = screen_w;
    area.h = IMG_LIST_ITEM_T_H;
    item = s_scr_list_p->lt_content->add_item((as_control*)s_scr_list_p->lt_content, area);
    item->set_bg_image((as_control*)item, as_ucs2_from_ascii(IMG_LIST_ITEM_T), AS_IMAGE_FROM_RES);
    item->set_bg_selected_image((as_control*)item, as_ucs2_from_ascii(IMG_LIST_ITEM_T), AS_IMAGE_FROM_RES);
    s_scr_list_p->lt_content->add_item_end((as_control*)s_scr_list_p->lt_content, item);

    if (bRecord)
        as_scr_list_add_record_items(s_scr_list_p->lt_content, record_p);
    else
        as_scr_list_add_category_items(s_scr_list_p->lt_content, idx_begin, idx_end);

    area.x = 0;
    area.y = s_scr_list_p->head_height + IMG_LIST_ITEM_T_H + s_scr_list_p->items_height;
    area.w = screen_w;
    area.h = IMG_LIST_ITEM_B_H;
    item = s_scr_list_p->lt_content->add_item((as_control*)s_scr_list_p->lt_content, area);
    item->set_bg_image((as_control*)item, as_ucs2_from_ascii(IMG_LIST_ITEM_B), AS_IMAGE_FROM_RES);
    item->set_bg_selected_image((as_control*)item, as_ucs2_from_ascii(IMG_LIST_ITEM_B), AS_IMAGE_FROM_RES);
    s_scr_list_p->lt_content->add_item_end((as_control*)s_scr_list_p->lt_content, item);
}
static void as_scr_list_add_content_list(as_screen* screen_p, ASINT categoryid)
{
    as_rect area;
    ASINT screen_w, screen_h;
    screen_w = as_get_lcd_width();
    screen_h = as_get_lcd_height();
    area.x = 0;
    area.y = s_scr_list_p->head_height;
    area.w = screen_w;
    area.h = screen_h - s_scr_list_p->head_height - BUTTON_BAR_H;
    s_scr_list_p->lt_content = (as_list*)screen_p->add_ctrl(screen_p, AS_CTRL_LIST, area, 0 , NULL);
    s_scr_list_p->lt_content->set_bg_color((as_control*)s_scr_list_p->lt_content, 0xAE1C1C);
    as_scr_list_add_content_list_items(screen_p, categoryid);
    s_scr_list_p->lt_content->set_selected_idx((as_control*)s_scr_list_p->lt_content, 1);
}
static void as_scr_list_add_content(as_screen* screen_p, ASINT categoryid)
{
    as_scr_list_add_content_list(screen_p, categoryid);
}

static void as_scr_list_lsk_evt(void)
{
    ASINT idx = s_scr_list_p->lt_content->select_idx;
    as_screen_manager* scr_mgr = as_get_screen_stack_instance();
    idx--;

    if (idx < 0 || idx >= AS_ITEMS_COUNT)
        return;

    idx = as_logic_translate_idx(idx, s_category_id);
    scr_mgr->clear_all();
    as_scr_detail_show(idx, s_category_id);
}
static void as_scr_list_rsk_evt(void)
{
    as_screen_manager* scr_mgr = as_get_screen_stack_instance();
    scr_mgr->clear_all();

    if (s_category_id >= ENUM_CATEGORY_SIXTY && s_category_id < ENUM_CATEGORY_COUNT)
        as_scr_category_show();
    else
        as_scr_main_show();
}

static void as_scr_list_add_softkey(as_screen* screen_p)
{
    as_screen_create_softkey_bar(screen_p, TEXT_ENTER, TEXT_BACK, as_scr_list_lsk_evt, as_scr_list_rsk_evt);
}
void as_scr_list_init(as_screen* screen_p)
{
    s_scr_list_p = (as_list_screen*)screen_p;
    screen_p->set_bg_color(screen_p, SCREEN_BG_COLOR);
    as_scr_list_add_head(screen_p, s_category_id);
    as_scr_list_add_content(screen_p, s_category_id);
    as_scr_list_add_softkey(screen_p);
}

void as_scr_list_deinit(as_screen* screen_p)
{
    AS_UNUSED(screen_p);
}

void as_scr_list_show(ASINT categoryid)
{
    as_screen_manager* scr_mgr = as_get_screen_stack_instance();
    as_screen* scr_p;
    as_ret ret;
    s_category_id = categoryid;
    ret = scr_mgr->new_and_push_screen(AS_SCREEN_LIST);
    scr_p = scr_mgr->get_cur_screen();

    if (scr_p)
    {
        scr_p->paint_hdlr(scr_p);
        scr_p->flush_hdlr(scr_p);
    }
}